<?php

declare(strict_types=1);

namespace App\Controller\AdminApi;

use App\Middleware\AdminJWTAuthMiddleware;
use Dotenv\Validator;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use App\Exception\XZXValidationException;
use App\Service\RiskRuleWhitetextParamService;
use App\Utils\CustomUtils;
use App\Utils\DateUtils;
use Psr\Container\ContainerInterface;
use Hyperf\Logger\LoggerFactory;

/**
 * Class RiskRuleWhitetextParamController
 *
 * @package App\Controller
 * @Controller(prefix="/admin/api/riskRuleWhitetextParam")
 * @Middleware(AdminJWTAuthMiddleware::class)
 */
class RiskRuleWhitetextParamController extends AdminBaseController {
    /**
     * @Inject()
     * @var ValidatorFactoryInterface
     */
    protected $validationFactory;

    /**
     * @Inject()
     * @var RiskRuleWhitetextParamService
     */
    protected $RiskRuleWhitetextParamService;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    public function __construct (LoggerFactory $loggerFactory) {

        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }

    /**
     * 查看
     * @PostMapping(path = "get")
     */
    public function get (RequestInterface $request) {

        $validator = $this->validationFactory->make($request->all(), [
            'id' => 'required',
        ],);

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $param = $request->all();

        return $this->RiskRuleWhitetextParamService->get($param);
    }

    /**
     * 编辑
     * @PostMapping(path = "edit")
     */
    public function edit (RequestInterface $request) {

        $validator = $this->validationFactory->make($request->all(), [
            'id'                   => 'required',
            'whiteType'            => 'required',
            'whiteTextName'        => 'required',
            'whiteTextDisplayName' => 'required',
            'whiteText'            => 'required',
        ],);

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $param = [
            'id'                   => $request->post('id'),
            'whiteType'            => $request->post('whiteType'),
            'whiteTextName'        => $request->post('whiteTextName'),
            'whiteTextDisplayName' => $request->post('whiteTextDisplayName'),
            'whiteText'            => $request->post('whiteText'),
        ];

        $ext = $this->request->getAttribute("ext");

        $param['createBy'] = $ext['user']['username'];
        $param['updateBy'] = $ext['user']['username'];

        return $this->RiskRuleWhitetextParamService->edit($param);
    }

    /**
     * 添加
     * @PostMapping(path = "add")
     */
    public function add (RequestInterface $request) {

        $validator = $this->validationFactory->make($request->all(), [
            'whiteType'            => 'required',
            'whiteTextName'        => 'required',
            'whiteTextDisplayName' => 'required',
            'whiteText'            => 'required',
        ],);

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $param = [
            'whiteType'            => $request->post('whiteType'),
            'whiteTextName'        => $request->post('whiteTextName'),
            'whiteTextDisplayName' => $request->post('whiteTextDisplayName'),
            'whiteText'            => $request->post('whiteText'),
        ];

        $ext = $this->request->getAttribute("ext");

        $param['createBy'] = $ext['user']['username'];
        $param['updateBy'] = $ext['user']['username'];

        return $this->RiskRuleWhitetextParamService->add($param);
    }

    /**
     * 删除
     * @PostMapping(path = "del")
     */
    public function del (RequestInterface $request) {

        $validator = $this->validationFactory->make($request->all(), [
                'id' => 'required',
            ],);

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $param = $request->all();


        return $this->RiskRuleWhitetextParamService->del($param);
    }

    /**
     * 列表
     * @PostMapping(path = "list")
     */
    public function list (RequestInterface $request) {

        $validator = $this->validationFactory->make($request->all(), [
                'page'  => 'required|integer|min:1',
                'limit' => 'required|integer|min:1',
            ],);

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $param = [
            'page'  => $request->post('page'),
            'limit' => $request->post('limit'),
        ];

        $search = $request->post('search', []);

        CustomUtils::xzxAddSearch($param, $search, 'whiteTextName');
        CustomUtils::xzxAddSearch($param, $search, 'whiteTextDisplayName');
        CustomUtils::xzxAddSearch($param, $search, 'whiteText');
        CustomUtils::xzxAddSearch($param, $search, 'whiteType');
        CustomUtils::xzxAddSearch($param, $search, 'action');
        CustomUtils::xzxAddSearch($param, $search, 'status');

        if (isset($param["createTimeRange"])) {
            try {
                $param['createTimeRange'][0] = DateUtils::dateToTimeStamp($param['createTimeRange'][0]);
                $param['createTimeRange'][1] = DateUtils::dateToTimeStamp($param['createTimeRange'][1]);
            } catch (\Throwable $th) {
                unset($param['createTimeRange']);
            }
        }


        return $this->RiskRuleWhitetextParamService->list($param);
    }

    /**
     * 选项列表
     * @PostMapping(path = "options")
     */
    public function options (RequestInterface $request) {

        $param = [
            'status' => 1,
        ];

        return $this->RiskRuleWhitetextParamService->options($param);
    }

    /**
     * 下拉框/白名单类型
     * @PostMapping(path = "whiteTypeOptions")
     */
    public function whiteTypeOptions (RequestInterface $request) {
        $param = [];
        return $this->RiskRuleWhitetextParamService->whiteTypeOptions($param);
    }

    /**
     * 下拉框/行为
     * @PostMapping(path = "actionOptions")
     */
    public function actionOptions (RequestInterface $request) {
        $param = [];
        return $this->RiskRuleWhitetextParamService->actionOptions($param);
    }


}
